﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace KTDS_IPBS_WEB
{
    public partial class Page2 : System.Web.UI.Page
    {
        C_DBConnect dbcon = new C_DBConnect();
        InfoOfTraceIP st_Result = new InfoOfTraceIP();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // ============================= 파싱 부분 ===============================

        protected void btn_ExcuteTrace_Click(object sender, EventArgs e)
        {
            List<string> lst_ParseIP = new List<string>();
            String InputText = TextBox1.Text;
            TextBox1.Text = null;
            lst_ParseIP = ParsingTracerouteText(InputText.Trim(), lst_ParseIP);

            List<string> lst_tablename = new List<string>();
            lst_tablename.Add("호스트ID");
            lst_tablename.Add("IP");
            lst_tablename.Add("호스트명");
            lst_tablename.Add("구간");
            lst_tablename.Add("장비종류");
            lst_tablename.Add("담당자이름");
            lst_tablename.Add("담당자부서");
            lst_tablename.Add("연락처");

            table2.Rows.Clear();

            TableRow row_top = new TableRow();
            for (int i = 0; i < lst_tablename.Count(); i++)
            {
                TableCell cell = new TableCell();
                cell.Text = lst_tablename[i].ToString();
                row_top.Cells.Add(cell);
            }
            table2.Rows.Add(row_top);

            List<string> lst_Result = new List<string>();
            for (int i = 0; i < lst_ParseIP.Count(); i++)
            {
                TableRow row = new TableRow();
                st_Result = dbcon.SearchInformationOfTraceIP(lst_ParseIP[i]);

                lst_Result.Clear();
                lst_Result.Add(st_Result.host_id);
                lst_Result.Add(lst_ParseIP[i]);
                lst_Result.Add(st_Result.host_name);
                lst_Result.Add(st_Result.loc);
                lst_Result.Add(st_Result.type);
                lst_Result.Add(st_Result.name);
                lst_Result.Add(st_Result.dept);
                lst_Result.Add(st_Result.phone);

                for (int y = 0; y < lst_Result.Count(); y++)
                {
                    TableCell cell = new TableCell();
                    if (lst_Result[y] == null)
                    {
                        cell.Text = "-";
                    }
                    else
                    {
                        cell.Text = lst_Result[y].ToString();
                    }
                    row.Cells.Add(cell);
                }

                table2.Rows.Add(row);
            }
        }

        private List<string> ParsingTracerouteText(String ParaString, List<string> lst_ParseIP)
        {
            String[] FirstSplitText = ParaString.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            //String[] SecondSplitText = new String[100];
            String[] IpInfo = new String[100];

            for (int i = 0; i < FirstSplitText.Length; i++)
            {
                String[] SecondSplitText = FirstSplitText[i].Split(' ');

                for (int y = 0; y < SecondSplitText.Length; y++)
                {
                    IpInfo[i] = SecondSplitText[y];

                    if (IpInfo[i].Contains('[') || IpInfo[i].Contains(']'))
                    {
                        IpInfo[i] = IpInfo[i].Replace('[', ' ').Trim();
                        IpInfo[i] = IpInfo[i].Replace(']', ' ').Trim();
                    }
                }
                TextBox1.Text += IpInfo[i].ToString() + Environment.NewLine;
                if (IpInfo[i].ToString() != null)
                {
                    lst_ParseIP.Add(IpInfo[i].ToString());
                }
            }

            return lst_ParseIP;
        }

    }
}